import Vue from 'vue'
import App from './App'

import store from './store'

import uView from 'uview-ui';
Vue.use(uView);

import customCommon from 'common/js/common.js'
Vue.use(customCommon)

Vue.config.productionTip = false

Vue.prototype.$store = store

// 查看服务器状态
Vue.prototype.checkServerStatus = function(userInfo){
	
}

// 登录函数
Vue.prototype.loginAction = function(userInfo){
	
	// 整理用户信息
	var userInfo = [
		['avatarurl', userInfo.avatarUrl],
		['city', userInfo.city],
		['country', userInfo.country],
		['gender', userInfo.gender],
		['language', userInfo.language],
		['nickname', userInfo.nickName],
		['province', userInfo.province]
	]
	
	let self = this;
	var loginUrl = getApp().globalData.loginUrl, terminal = getApp().globalData.terminal;
	uni.login({
		success: (loginRes) => {
			uni.request({
				url: loginUrl,
				method: 'POST',
				data: {
					code: loginRes.code,
					terminal: terminal,
					userInfo: userInfo
				},
				success: (res) => {
					if(res.data.status == 200){
						// 存储jwt
						uni.setStorageSync('userJWT', res.data.data.jwt);
						uni.setStorageSync('hasLogin', true);
						self.$store.commit('login');
					}else{
						this.showToastCustom(res.data.txt);
					}
				},
				fail: (res) => {
					this.showToastCustom(res.errMsg);
				}
			});
		},
		fail: (loginRes) => {
			this.showToastCustom(loginRes.errMsg);
		}
	});
}

// 注销函数
Vue.prototype.logoutAction = function(){
	uni.removeStorageSync('userJWT');
	uni.removeStorageSync('hasLogin');
	this.$store.commit('logout');
}

// 检查全局变量的服务端的登录状态是否正确
Vue.prototype.checkhasLoginAction = function(){
	console.log('check hasLogin status');
	const userJWT = uni.getStorageSync('userJWT');
	const hasLogin = uni.getStorageSync('hasLogin');
	if(userJWT !== '' && hasLogin){
		this.$store.commit('login');
		return true;
	}else{
		this.$store.commit('logout');
		return false;
	}
}

// 以同步方式进行检测
// 检测jwt是否过期，若过期则重新授权登录
Vue.prototype.checkJwtAction = async function(){
	console.log('check JWT status');
	var checkJwtUrl = getApp().globalData.checkJwtUrl;
	var jwt = uni.getStorageSync('userJWT');
	if(jwt != ''){
		var [error, res] = await uni.request({
			url: checkJwtUrl,
			method: 'POST',
			data: {
				jwt: jwt
			}
		});
		if(error != null){
			this.showToastCustom(error.errMsg);
		}else{
			if(res.data.status == 250){
				// 无效 且 jwt不为空(即曾授权)
				this.logoutAction();
				// 重新发起授权,曾授权过的小程序可以调用该接口
				var [uerror, ures] = await uni.getUserInfo()
				if(uerror != null){
					this.showToastCustom(uerror.errMsg);
				}else{
					console.log('JWT over time');
					console.log('get userInfo again');
					this.loginAction(ures.userInfo);
				}
			}
		}
	}
}

Vue.prototype.serverDie = function(msg){
	msg = msg != undefined ? (msg == 'request:fail ' ? '服务器挂掉了' : msg) : '';
	return msg;
}

// 普通提示框
Vue.prototype.showToastCustom = function(msg, icon){
	msg = this.serverDie(msg);
	icon = icon != undefined ? icon : 'none';
	uni.showToast({
		title: msg,
		duration: 1000,
		icon: icon
	});
}

// 从服务器获取当前时间，避免机型差异
// 以同步形式获取，在onLoad调用处需要注意: 
// 1. onLoad: async () =>{}
// 2. await self.getCurTimeFromServer();
// (已弃用)
// Vue.prototype.getCurTimeFromServer = async function(){
// 	var getTimeUrl = getApp().globalData.getTimeUrl;
// 	var [error, res] = await uni.request({
// 		url: getTimeUrl,
// 		data: {
// 			offset: '+30min'
// 		}
// 	});
// 	if(res.data.status == 200){
// 		this.$store.commit('changeCurTime', res.data.data);
// 	}
// }

App.mpType = 'app'

const app = new Vue({
	store,
	...App,
})
app.$mount()
